package CGW.service;

import CGW.bean.Dingdan;
import CGW.bean.Page;
import CGW.dao.DingdanDao;

import java.util.List;

public class DingdanService {
    private DingdanDao dao;

    public DingdanService(DingdanDao dao) {
        this.dao = dao;
    }

    public List<Dingdan> queryAll(){
        List<Dingdan> ret = dao.all(1, 100);
        dao.close();
        return ret;
    }

    public Page<Dingdan> queryAll(int page, int pageSize){
        int total = dao.total();
        List<Dingdan> ret = dao.all(page, pageSize);
        Page<Dingdan> pageBean = new Page<>(ret, page, (int)Math.ceil(total*1.0/pageSize), pageSize);
        dao.close();
        return pageBean;
    }


    public boolean insert(Dingdan dingdan){
        int rows = dao.add(dingdan);
        dao.commit();
        dao.close();
        return rows > 0;
    }


    public boolean update(Dingdan dingdan){
        int rows = dao.update(dingdan);
        dao.commit();
        dao.close();
        return  rows > 0;
    }
    public boolean delete(int order_id){
        int rows = dao.delete(order_id);
        dao.commit();
        dao.close();
        return  rows > 0;
    }

    public void close(){
        dao.close();
    }
}
